<template>
  <div>
    <!-- 添加或修改班级对话框 -->
    <el-drawer with-header :visible.sync="open_schoolclass" direction="rtl" size="25%" :show-close="false"
      :wrapperClosable="false">




      <el-row :gutter="20">
        <el-col :span="12">
          <div class="ml30">
            <div class="     text-size-40  " style="font-weight: 600;">创建班级</div>

            <div class="       text-size-17 mt20 text-gray  ">创建后记得添加学员和排课哦</div>

          </div>

        </el-col>
        <el-col :span="12">
          <div class="classflex-end mt10">

            <el-button @click="cancel" style="width: 100px;margin-right: 100px;">取消</el-button>

          </div>

        </el-col>
      </el-row>



      <el-form :hide-required-asterisk="true" class=" ml30 mr30" ref="form" :model="form" :rules="rules"
        label-width="80px" label-position="top">
        <el-divider style="width: 80%;"></el-divider>
        <div>
          <div class="grid-content bg-purple    text-size-20-mt10-black  ">班级名称</div>
        </div>
        <div>
          <div class="grid-content bg-purple    text-size-17 mt20 text-gray  ">例如:初级班、周一10点班、周末机动班、....</div>
        </div>
        <el-form-item label=" " prop="className">
          <el-input style="width: 450px;" v-model="form.className" placeholder="请输入班级名称" />
        </el-form-item>
        <div>
          <div class="grid-content bg-purple    text-size-20-mt10-black  ">关联课程</div>
        </div>
        <div>
          <div class="grid-content bg-purple   text-size-17 mt20 text-gray  ">关联后，班级将归属于课程下</div>
        </div>
        <el-form-item label=" " prop="classCourse">




          <el-select style="width: 300px;" v-model="form.classCourse" placeholder="请选择报名课程" filterable>
            <el-option v-for="dict in courseList" :key="dict.id" :label="dict.courseName" :value="dict.courseName">
            </el-option>

          </el-select>

        </el-form-item>
        <div>
          <div class="grid-content bg-purple    text-size-20-mt10-black  ">班级容量(人)</div>
        </div>
        <div>
          <div class="grid-content bg-purple    text-size-17 mt20 text-gray  ">限制班级在读学员数量</div>
        </div>
        <el-form-item label=" " prop="classCapacity">
          <el-input-number v-model=" classCapacity" :min="1" :max="200" label="描述文字" controls-position="right"></el-input-number>


        </el-form-item>
        <el-divider></el-divider>
        <div>
          <el-button type="primary" @click="submitForm" style="width: 100%;">立即创建</el-button>
          <!-- <el-button @click="cancel">取 消</el-button> -->
        </div>
      </el-form>

    </el-drawer>

  </div>
</template>

<script>
  import {
    listSchoolclass,
    getSchoolclass,
    delSchoolclass,
    addSchoolclass,
    updateSchoolclass
  } from "@/api/schoolclass/schoolclass";
  import {
    listCourse,
    getCourse,
    delCourse,
    addCourse,
    updateCourse
  } from "@/api/course/course";
  export default {
    name: "Schoolclass",
    dicts: ['class_circulate'],
    data() {
      return {
        classCapacity:1,
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 班级表格数据
        schoolclassList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open_schoolclass: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          deptId: null,
          className: null,
          classCourse: null,
          classCapacity: null,
          classAskforleave: null,
          classNoyet: null,
          classWorkOut: null,
          classCirculate: null,
          classStart: null,
          classFinish: null,
          classHours: null,
          classTeacher: null,
          classAttendant: null,
          classRoom: null,
          classStudent: null,
          remark2: null,
          remark3: null,
          remark4: null,
          remark5: null,
          remark6: null,
          remark7: null,
          remark8: null,
          remark9: null,
          remark10: null
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          className: [{
              required: true,
              message: '请输入班级名称',
              trigger: 'blur'
            },
            {
              min: 1,
              max: 15,
              message: '长度在 1 到 15 个字符',
              trigger: 'blur'
            }
          ],
          classCourse: [{
            required: true,
            message: '请选择关联课程',
            trigger: 'change'
          }],





        },
        courseList: [],
        queryParamsCourse: {},
      };
    },
    created() {
      
      this.getList();
      this.getCourseList();
    },
    methods: {
      /** 查询课程管理列表 */
      getCourseList() {
        this.queryParamsCourse.deptId = this.$store.state.user.deptId;
		  this.queryParamsCourse.courseState ="在开课程";
        listCourse(this.queryParamsCourse).then(response => {
          this.courseList = response.rows;

        });
      },
      open() {
        this.open_schoolclass = true;

      },
      /** 查询班级列表 */
      getList() {
        this.loading = true;
        this.queryParams.deptId = this.$store.state.user.deptId;
        listSchoolclass(this.queryParams).then(response => {
          this.schoolclassList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open_schoolclass = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          deptId: null,
          className: null,
          classCourse: null,
          classCapacity: 1,
          classAskforleave: null,
          classNoyet: null,
          classWorkOut: null,
          classCirculate: null,
          classStart: null,
          classFinish: null,
          classHours: null,
          classTeacher: null,
          classAttendant: null,
          classRoom: null,
          classStudent: null,
          delFlag: null,
          createBy: null,
          createTime: null,
          updateBy: null,
          updateTime: null,
          remark: null,
          remark2: null,
          remark3: null,
          remark4: null,
          remark5: null,
          remark6: null,
          remark7: null,
          remark8: null,
          remark9: null,
          remark10: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open_schoolclass = true;
        this.title = "添加班级";
        this.form.deptId = this.$store.state.user.deptId;
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const id = row.id || this.ids
        getSchoolclass(id).then(response => {
          this.form = response.data;
          this.open_schoolclass = true;
          this.title = "修改班级";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            this.form.deptId = this.$store.state.user.deptId;
             this.form.classCapacity=this.classCapacity;
            if (this.form.id != null) {

              updateSchoolclass(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open_schoolclass = false;
                // this.getList();
              });
            } else {
              addSchoolclass(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open_schoolclass = false;
                this.$emit('AddSchoolclassDrawer', 'addsuccess'); // 触发名为'childEvent'的自定义事件，并传递数据
                this.reset();
                // this.getList();


              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$modal.confirm('是否确认删除班级编号为"' + ids + '"的数据项？').then(function() {
          return delSchoolclass(ids);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('schoolclass/schoolclass/export', {
          ...this.queryParams
        }, `schoolclass_${new Date().getTime()}.xlsx`)
      }
    }
  };
</script>
